#include <iostream>
#include "key_tree_view.h"
#include "main_form.h"


Key_Tree_View::Key_Tree_View()
{
	//create_popup_menu();
	//Fill popup menu:
	{
		Gtk::Menu::MenuList & menulist = popup_menu_.items();

		menulist.push_back( Gtk::Menu_Helpers::MenuElem("展开",
			sigc::mem_fun(*this, &Key_Tree_View::on_expanded) ) );
		menulist.push_back( Gtk::Menu_Helpers::MenuElem("新建(_N)",
			sigc::mem_fun(*this, &Key_Tree_View::on_create_key) ) );
		menulist.push_back( Gtk::Menu_Helpers::MenuElem("查找(_F)",
			sigc::mem_fun(*this, &Key_Tree_View::on_create_key) ) );
		menulist.push_back( Gtk::Menu_Helpers::MenuElem("删除(_D)",
			sigc::mem_fun(*this, &Key_Tree_View::on_delete) ) );
		menulist.push_back( Gtk::Menu_Helpers::MenuElem("重命名(_R)",
			sigc::mem_fun(*this, &Key_Tree_View::on_create_key) ) );			
		menulist.push_back( Gtk::Menu_Helpers::MenuElem("导出(_E)",
			sigc::mem_fun(*this, &Key_Tree_View::on_key_path_exported) ) );	
		menulist.push_back( Gtk::Menu_Helpers::MenuElem("复制项名称(_C)",
			sigc::mem_fun(*this, &Key_Tree_View::on_key_path_copy) ) );					
		Gtk::MenuItem new_item_root("test item");
		
		//new_item_root->add_label("新建(_N)");
		
		Gtk::Menu* new_item_submenu = Gtk::manage(new Gtk::Menu());
		//new_item_root->set_submenu(*new_item_submenu);
		//new_item_submenu->set_title("ddd");
		
		
		new_item_submenu->items().push_back(
		Gtk::Menu_Helpers::MenuElem("新建项(_K)", sigc::mem_fun(*this, &Key_Tree_View::on_create_key)));
		new_item_root.set_submenu(*new_item_submenu);
		popup_menu_.append(new_item_root);
	}
	popup_menu_.accelerate(*this);
}

Key_Tree_View::~Key_Tree_View()
{
}

bool 
Key_Tree_View::on_button_release_event(GdkEventButton* event)
{
	Gtk::TreeView::on_button_release_event(event);
	
  if( (event->type == GDK_BUTTON_RELEASE) && (event->button == 3) )
  {
   Glib::RefPtr<Gtk::TreeView::Selection> refSelection = get_selection();
	if(refSelection)
   {
   	 Gtk::TreeModel::iterator iter = refSelection->get_selected();
    	if(iter) {
   		printf("have a seletion\n");
      	popup_menu_.popup(event->button, event->time);
		} /*else {   	
			printf("haven't a seletion\n");
			new_item_popup_menu_.popup(event->button, event->time);	
		}*/
	} 

    return true; //It has been handled.
  }
  else
    return false;
}

void 
Key_Tree_View::on_create_key()
{
  std::cout << "A popup menu item was selected." << std::endl;
}

void
Key_Tree_View::on_expanded()
{
	Glib::RefPtr<Gtk::TreeView::Selection> refSelection = get_selection();
	if(refSelection)
	{
		Gtk::TreeModel::iterator iter = refSelection->get_selected();
		if(iter) {
			Gtk::TreeModel::Path path(iter);
			if (row_expanded(path)) { // 键已经展开
				collapse_row(path);
			} else {
				expand_to_path(path);
			}
		}
	}
	std::cout << "row expanded." << std::endl;
}

/*void	Key_Tree_View::on_modify()
{
	Glib::RefPtr<Gtk::TreeView::Selection> refSelection = get_selection();
	if(refSelection)
	{
		Gtk::TreeModel::iterator iter = refSelection->get_selected();
		if(iter)
		{
			Gtk::TreeModel::Path path(iter);
			edit_selected_(path, get_column(*path.begin()));
		}
	}
}*/

void	
Key_Tree_View::on_delete()
{
	Glib::RefPtr<Gtk::TreeView::Selection> refSelection = get_selection();
	if(refSelection)
	{
		Gtk::TreeModel::iterator iter = refSelection->get_selected();
		if(iter)
		{
			delete_key_selected_(iter);
		}
	}
}

void Key_Tree_View::on_key_path_copy()
{
	Glib::RefPtr<Gtk::TreeView::Selection> refSelection = get_selection();
	if(refSelection)
	{
		Gtk::TreeModel::iterator iter = refSelection->get_selected();
		if(iter)
		{
			key_whole_path_copy_(iter);
		}
	}	
}

void Key_Tree_View::on_key_path_exported()
{
	Glib::RefPtr<Gtk::TreeView::Selection> refSelection = get_selection();
	if(refSelection)
	{
		Gtk::TreeModel::iterator iter = refSelection->get_selected();
		if(iter)
		{
			key_path_exported_(iter);
		}
	}
}

/*
void
Key_Tree_View::on_rename()
{
}*/
